package com.littleken.foodieService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.littleken.foodieCommon.Utils.DesensitizationUtil;
import com.littleken.foodieMapper.ItemsCommentsMapper;
import com.littleken.foodieMapper.ItemsImgMapper;
import com.littleken.foodieMapper.ItemsMapper;
import com.littleken.foodieMapper.ItemsParamMapper;
import com.littleken.foodieMapper.ItemsSpecMapper;
import com.littleken.foodiePojo.domain.ItemsCommentsDO;
import com.littleken.foodiePojo.domain.ItemsDO;
import com.littleken.foodiePojo.domain.ItemsImgDO;
import com.littleken.foodiePojo.domain.ItemsParamDO;
import com.littleken.foodiePojo.domain.ItemsSpecDO;
import com.littleken.foodiePojo.enums.CommentLevelEnum;
import com.littleken.foodiePojo.enums.ImgIsMainEnum;
import com.littleken.foodiePojo.vo.CommentLevelCountsVO;
import com.littleken.foodiePojo.vo.ItemCommentVO;
import com.littleken.foodiePojo.vo.SearchItemsVO;
import com.littleken.foodiePojo.vo.ShopcartVO;
import com.littleken.foodieService.ItemService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * Create by littleken on 2020/11/24 13:59
 */
@Service
public class ItemServiceImpl implements ItemService {

    /**
     * 商品mapper
     */
    @Resource
    private ItemsMapper itemsMapper;

    /**
     * 商品图片mapper
     */
    @Resource
    private ItemsImgMapper itemsImgMapper;

    /**
     * 商品规格mapper
     */
    @Resource
    private ItemsSpecMapper itemsSpecMapper;

    /**
     * 商品详情mapper
     */
    @Resource
    private ItemsParamMapper itemsParamMapper;

    /**
     * 商品评价mapper
     */
    @Resource
    private ItemsCommentsMapper itemsCommentsMapper;


    @Override
    public ItemsDO getItemById(String itemId) {
        ItemsDO itemsDO = null;
        itemsDO = itemsMapper.selectById(itemId);
        return itemsDO;
    }

    @Override
    public List<ItemsImgDO> getItemImgList(String itemId) {
        List<ItemsImgDO> itemsImgDOS = Lists.newArrayList();
        QueryWrapper<ItemsImgDO> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        itemsImgDOS = itemsImgMapper.selectList(wrapper);
        return itemsImgDOS;
    }

    @Override
    public List<ItemsSpecDO> getItemSpecList(String itemId) {
        List<ItemsSpecDO> itemsSpecDOS = Lists.newArrayList();
        QueryWrapper<ItemsSpecDO> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        itemsSpecDOS = itemsSpecMapper.selectList(wrapper);
        return itemsSpecDOS;
    }

    @Override
    public ItemsParamDO getItemParam(String itemId) {
        QueryWrapper<ItemsParamDO> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        ItemsParamDO itemsParamDO = itemsParamMapper.selectOne(wrapper);
        return itemsParamDO;
    }

    /**
     * 根据商品id查询商品的评价等级数量
     *
     * @param itemId
     */
    @Override
    public CommentLevelCountsVO getCommentCounts(String itemId) {
        Integer goodCount = getCommentCounts(itemId, CommentLevelEnum.GOOD.getCode());
        Integer normalCount = getCommentCounts(itemId, CommentLevelEnum.NORMAL.getCode());
        Integer badCount = getCommentCounts(itemId, CommentLevelEnum.BAD.getCode());
        Integer totalCount = goodCount + normalCount + badCount;
        CommentLevelCountsVO countVO =
                new CommentLevelCountsVO(totalCount, goodCount, normalCount, badCount);
        return countVO;
    }

    /**
     * 根据商品id查询商品的评价（分页）
     *
     * @param itemId
     * @param level
     * @return
     */
    @Override
    public List<ItemCommentVO> getAllComments(String itemId, Integer level) {
        List<ItemCommentVO> itemComments = Lists.newArrayList();

        Map<String, Object> map = new HashMap<>();
        map.put("itemId", itemId);
        map.put("level", level);
        itemComments = itemsMapper.getItemComments(map);
        //数据脱敏
        for (ItemCommentVO vo : itemComments) {
            vo.setNickname(DesensitizationUtil.commonDisplay(vo.getNickname()));
        }
        return itemComments;
    }

    @Override
    public List<SearchItemsVO> searhItemsByKeyword(String keywords, String sort) {
        List<SearchItemsVO> searchItemsVOS = Lists.newArrayList();
        Map<String, Object> map = new HashMap<>();
        map.put("keywords", keywords);
        map.put("sort", sort);
        searchItemsVOS = itemsMapper.searchItems(map);
        return searchItemsVOS;
    }

    @Override
    public List<SearchItemsVO> searhItemsByCatId(Integer catId, String sort) {
        List<SearchItemsVO> searchItemsVOS = Lists.newArrayList();
        Map<String, Object> map = new HashMap<>();
        map.put("catId", catId);
        map.put("sort", sort);
        searchItemsVOS = itemsMapper.searchItemsByThirdCat(map);
        return searchItemsVOS;
    }

    @Override
    public List<ShopcartVO> queryItemsBySpecIds(String specIds) {
        return null;
    }

    /**
     * 根据评价类型查询评价数量
     *
     * @param itemId
     * @param level
     * @return
     */
    private Integer getCommentCounts(String itemId, Integer level) {
        if (itemId == null || level == null ||
                itemsMapper.selectById(itemId) == null) {
            return new Integer(0);
        }
        QueryWrapper<ItemsCommentsDO> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        wrapper.eq("comment_level", level);
        return itemsCommentsMapper.selectCount(wrapper);
    }

    @Override
    public ItemsSpecDO queryItemSpecById(String specId) {
        return null;
    }

    @Override
    public String queryItemMainImgById(String itemId) {
        QueryWrapper<ItemsImgDO> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id", itemId);
        wrapper.eq("is_main", ImgIsMainEnum.YES.getCode());
        ItemsImgDO itemsImgDO = itemsImgMapper.selectOne(wrapper);
        return itemsImgDO != null ? itemsImgDO.getUrl() : "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean decreaseItemSpecStock(String specId, int buyCounts) {
        ItemsSpecDO itemsSpecDO = itemsSpecMapper.selectById(specId);
        int count = itemsSpecDO.getStock() - buyCounts;
        if (count < 0) {
            return false;
        }
        itemsSpecDO.setStock(count);
        itemsSpecDO.setUpdatedTime(LocalDate.now());
        int i = itemsSpecMapper.updateById(itemsSpecDO);
        return i == 1 ? true : false;
    }
}
